package com.cait.dutyfree.cart.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.cait.dutyfree.cart.CartProcessor;
import com.cait.dutyfree.dataaccess.InventoryServiceDAO;
import com.cait.dutyfree.pojo.Product;
import com.cait.dutyfree.pojo.ShoppingCartItem;
import com.cait.dutyfree.pojo.Stock;

public class CartProcessorImpl implements CartProcessor {
	private InventoryServiceDAO inventoryServiceDAO;

	// spring injection
	public InventoryServiceDAO getInventoryServiceDAO() {
		return inventoryServiceDAO;
	}

	public void setInventoryServiceDAO(InventoryServiceDAO inventoryServiceDAO) {
		this.inventoryServiceDAO = inventoryServiceDAO;
	}

	// spring injection ends

	@Override
	public void addToCart(Product p, int quantity, Map<String, Object> session) {
		if (session.containsKey("shoppingCart")
				&& (session.get("shoppingCart") instanceof List)) {
			@SuppressWarnings("unchecked")
			List<ShoppingCartItem> lst = (List<ShoppingCartItem>) session
					.get("shoppingCart");
			if (this.getExistingShoppingCartItem(lst, p) != null) {
				ShoppingCartItem itm = this.getExistingShoppingCartItem(lst, p);
				itm.setCount(itm.getCount() + quantity);
				// lst.add(itm);
			} else {
				ShoppingCartItem itm = new ShoppingCartItem();
				itm.setCount(quantity);
				itm.setProduct(p);
				lst.add(itm);
			}
			session.put("shoppingCart", lst);
		} else {
			List<ShoppingCartItem> lst = new ArrayList<ShoppingCartItem>();
			ShoppingCartItem itm = new ShoppingCartItem();
			itm.setCount(quantity);
			itm.setProduct(p);
			lst.add(itm);
			session.put("shoppingCart", lst);
		}
	}

	@Override
	public void removeFromCart(Product p, Map<String, Object> session) {
		if (session.containsKey("shoppingCart")
				&& (session.get("shoppingCart") instanceof List)) {
			@SuppressWarnings("unchecked")
			List<ShoppingCartItem> lst = (List<ShoppingCartItem>) session
					.get("shoppingCart");
			ShoppingCartItem itm = this.getExistingShoppingCartItem(lst, p);
			lst.remove(itm);
			session.put("shoppingCart", lst);
		}
	}

	@Override
	public List<ShoppingCartItem> getAllCartItems(Map<String, Object> session) {
		@SuppressWarnings("unchecked")
		List<ShoppingCartItem> lst = (List<ShoppingCartItem>) session
				.get("shoppingCart");
		return lst;
	}

	public void updateCart(Product p, Map<String, Object> session, Integer count) {
		if (session.containsKey("shoppingCart")
				&& (session.get("shoppingCart") instanceof List)) {
			@SuppressWarnings("unchecked")
			List<ShoppingCartItem> lst = (List<ShoppingCartItem>) session
					.get("shoppingCart");
			ShoppingCartItem itm = this.getExistingShoppingCartItem(lst, p);
			System.out.println("count: " + count);
			itm.setCount(count);
		}
	}

	public boolean isEnoughStockAvailable(Product product,
			Map<String, Object> session, Integer askedQuantity) {
		Stock stock = null;
		try {
			stock = this.getInventoryServiceDAO().getStock(product);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// logic goes here
		if (stock == null) {
			return false;
		} else {
			if (session.containsKey("shoppingCart")
					&& (session.get("shoppingCart") instanceof List)) {
				@SuppressWarnings("unchecked")
				List<ShoppingCartItem> lst = (List<ShoppingCartItem>) session
						.get("shoppingCart");
				ShoppingCartItem itm = this.getExistingShoppingCartItem(lst,
						stock.getProduct());
				if (itm == null) {
					if (askedQuantity > stock.getQuantity()) {
						return false;
					} else {
						return true;
					}
				} else {
					if ((itm.getCount() + askedQuantity) > stock.getQuantity()) {
						return false;
					} else {
						return true;
					}
				}

			} else {
				if (askedQuantity > stock.getQuantity()) {
					return false;
				} else {
					return true;
				}
			}
		}

	}

	private ShoppingCartItem getExistingShoppingCartItem(
			List<ShoppingCartItem> lst, Product p) {
		ShoppingCartItem itmToReturn = null;
		for (int i = 0; i < lst.size(); i++) {
			ShoppingCartItem itm = lst.get(i);
			if (itm.getProduct().equals(p)) {
				itmToReturn = itm;
				break;
			}
		}
		return itmToReturn;
	}

	@Override
	public void setAirline(Integer airlineId,  Map<String, Object> session) {
		
	}

}
